// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cfixedframe.h"
#include "ccube.h"

#define MOVE_STEP .4
#define ANGLE_STEP 0.2
#define PI 3.1416f

float x=0.0f;
float y=0.0f;
float z=10.0f;
float roll= 0.0f;
float pitch= 0.0f;
float yaw= 0.0f;
int w;
int h;
float cubeAngle= 0.f;

//Variable del cubo
CCube g_ccube;
CCube g_ccube2;

//variables booleanas para saber que tecla se ha pulsado
bool gbkey_w = false;
bool gbkey_s = false;
bool gbkey_a = false;
bool gbkey_d = false;



void Keyboard(unsigned char key,int x, int y)
{
	//keyboard callback function
	switch (key)
	{
	//foward
	case 'w':	
		gbkey_w = true;
		//g_x-= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z-= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//backward
	case 's':	
		gbkey_s = true;
		//g_x+= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z+= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//right
	case 'd': 
		gbkey_d = true;
		//g_yaw-= ANGLE_STEP; 
		break;
	//left
	case 'a':
		gbkey_a = true;
		//g_yaw+= ANGLE_STEP;
		break;
	case 27: exit(0);
	}
}
void KeyboardUp(unsigned char key,int x, int y)
{
	//keyboard callback function
	switch (key)
	{
	//foward
	case 'w':	
		gbkey_w = false;
		//g_x-= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z-= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//backward
	case 's':	
		gbkey_s = false;
		//g_x+= MOVE_STEP*sin(g_yaw*PI/180);
		//g_z+= MOVE_STEP*cos(g_yaw*PI/180);
		break;
	//right
	case 'd': 
		gbkey_d = false;
		//g_yaw-= ANGLE_STEP; 
		break;
	//left
	case 'a': //
		gbkey_a = false;
		//g_yaw+= ANGLE_STEP; break;
		break;
	case 27: exit(0);
	}
}

void Set3DView()
{
	//set projection matrix
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

	//set modelview matrix
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	glRotatef(-roll, 0.0, 0.0, 1.0);	
	glRotatef(-yaw, 0.0, 1.0, 0.0);
	glRotatef(-pitch, 1.0, 0.0, 0.0);	
	glTranslatef(-x, -y, -z);
}


	


void DrawScene(void)
{
	//clean the backbuffer
	glClear (GL_COLOR_BUFFER_BIT);

	//viewing transformation
	Set3DView();

	//draw the cube 
	//g_ccube.draw();
	glPushMatrix();
	/*g_ccube.setColor(0.0,155.0,0.0);*/
	g_ccube.setPosition(0.0,0.5,0.0);
	g_ccube.setScale(0.5, 0.5, 0.5);
	/*g_ccube.setRotation(10.0,1.0,1.0,0.0);*/
	g_ccube.draw();
	glPopMatrix();

	//draw cube2
	glPushMatrix();
	/*g_ccube2.setColor(155.0,0.0,0.0);*/
	g_ccube2.setPosition(0.0,0.0,-3.0);
	g_ccube2.setScale(2.0, 2.0, 2.0);
	/*g_ccube.setRotation(30.0,0.0,1.0,1.0);*/
	g_ccube2.draw();
	glPopMatrix();

}

void Reshape (int w, int h)
{
	//Reshape callback function
	h= h;
	w= w;
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
}

int main(int argc, char** argv)
{

	//INIT GLUT/////////////////////
	////////////////////////////////
	//init window and OpenGL context
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glEnable(GL_DEPTH_TEST);
	glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize (1024, 768); 
	glutCreateWindow (argv[0]);
//	glutFullScreen();


	//callback functions
	glutDisplayFunc(DrawScene);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);
	glutKeyboardUpFunc(KeyboardUp);

	CFixedFrameRate *pFFR;
	pFFR = new CFixedFrameRate(120);
	
	while (1)
	{
		//KEYSTROKES////////////////
		////////////////////////////
		if(gbkey_w)
		{
			x-= MOVE_STEP*sin(yaw*PI/180);
			z-= MOVE_STEP*cos(yaw*PI/180);
		}
		if(gbkey_s)
		{
			x+= MOVE_STEP*sin(yaw*PI/180);
			z+= MOVE_STEP*cos(yaw*PI/180);
		}
		if(gbkey_d)
		{
			yaw-= ANGLE_STEP;
		}
		if(gbkey_a)
		{
			yaw+= ANGLE_STEP;
		}


		//UPDATE////////////////////
		////////////////////////////
		//"move" the cube
		cubeAngle+= 0.1;
		//queued events?
		glutMainLoopEvent();


		//RENDER////////////////////
		////////////////////////////
		glutPostRedisplay();
		glutSwapBuffers();

		pFFR->endFrame();
	
		
	
	}
  return 0;
}